package com.mingmen.canting.caigourenyuan;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.mingmen.canting.R;
import com.mingmen.canting.adapter.CaiGouiLeftAdapter;
import com.mingmen.canting.definerecycleview.SwipeRecyclerView;
import com.mingmen.canting.model.ZhenBanShenHe;
import com.mingmen.canting.util.SharePreferenceUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

@SuppressLint("ValidFragment")
public class CaiGouRenZhongFragment extends Fragment implements CaiGouiLeftAdapter.OnItemClickListener {


    Unbinder unbinder;
    private String mTitle;
    //Fragment的View加载完毕的标记
    private boolean isLoading = false;

    //Fragment对用户可见的标记
    private boolean isUIVisible;


    TimePickerView pvTime;
    @BindView(R.id.textview_calendafirst)
    TextView textview_calendafirst;
    @BindView(R.id.textview_calendasecond)
    TextView textview_calendasecond;

    public static CaiGouRenZhongFragment getInstance(String title) {
        CaiGouRenZhongFragment sf = new CaiGouRenZhongFragment();
        sf.mTitle = title;
        return sf;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    String token;
    List<ZhenBanShenHe.DataBean> dataBeanList = new ArrayList<>();

    @BindView(R.id.layout_nodata)
    LinearLayout layout_nodata;
    @BindView(R.id.layout_calenda_first)
    LinearLayout layout_calenda_first;
    @BindView(R.id.layout_calenda_second)
    LinearLayout layout_calenda_second;


    @BindView(R.id.recyclerview_left)
    SwipeRecyclerView recyclerviewLeft;
    LinearLayoutManager linearLayoutManager_left;
    CaiGouiLeftAdapter caiGouiLeftAdapter;
    List<String> stringList = new ArrayList<>();
    CaiGouDetailFragment caiGouDetailFragment;
    int myposition = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fr_renyuanzhong, null);
        TextView card_title_tv = (TextView) v.findViewById(R.id.card_title_tv);
        card_title_tv.setText(mTitle);

        unbinder = ButterKnife.bind(this, v);
        token = (String) SharePreferenceUtil.get(getActivity(), "token", "");
        Log.d("Caigouzhong", "caigouzhong");
        initView();
        getCaneda();
        linearLayoutManager_left = new LinearLayoutManager(getActivity());
        recyclerviewLeft.setLayoutManager(linearLayoutManager_left);

        caiGouiLeftAdapter = new CaiGouiLeftAdapter(getActivity(), stringList);
        recyclerviewLeft.setAdapter(caiGouiLeftAdapter);
        caiGouiLeftAdapter.setOnItemClickListener(this);
        for (int i = 0; i < 5; i++) {
            stringList.add("Item" + i);
        }
        caiGouiLeftAdapter.setDataList(stringList);
        caiGouiLeftAdapter.setCheckedPosition(myposition);
        createFragment();

        return v;
    }

    public void createFragment() {
        FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
        caiGouDetailFragment = new CaiGouDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putString("right", stringList.get(myposition));
        caiGouDetailFragment.setArguments(bundle);
        fragmentTransaction.add(R.id.frame_layout, caiGouDetailFragment);
        fragmentTransaction.commit();
    }


    public void getCaneda() {


        Calendar selectedDate = Calendar.getInstance();//系统当前时间
        Calendar startDate = Calendar.getInstance();
        startDate.set(2014, 1, 23);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2027, 2, 28);
        pvTime = new TimePickerBuilder(getActivity(), new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                Toast.makeText(getActivity(), getTime(date), Toast.LENGTH_SHORT).show();
                switch (v.getId()) {
                    case R.id.layout_calenda_first:
                        textview_calendafirst.setText(getTime(date));
                        break;
                    case R.id.layout_calenda_second:
                        textview_calendasecond.setText(getTime(date));
                        break;
                }


            }
        }).setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setLayoutRes(R.layout.pickerview_custom_lunar, new CustomListener() {

                    @Override
                    public void customLayout(final View v) {
                        final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
                        ImageView ivCancel = (ImageView) v.findViewById(R.id.iv_cancel);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvTime.returnData();
                                pvTime.dismiss();
                            }
                        });
                        ivCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvTime.dismiss();
                            }
                        });


                    }


                })
                .setType(new boolean[]{true, true, true, false, false, false})
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setDividerColor(getResources().getColor(R.color.canedacolor))

                .build();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            isUIVisible = true;
            lazyLoad();//调用下面的方法
        } else {
            isUIVisible = false;
        }
    }

    private void lazyLoad() {
        if (!isLoading && isUIVisible) {
//            这里只加载一次数据
            Log.d("SimpleCardFragment", "66666");
            //数据加载完毕,恢复标记,防止重复加载
            isLoading = true;
            isUIVisible = false;
        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    private void initView() {
    }



    @OnClick({R.id.layout_calenda_first, R.id.layout_calenda_second})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.layout_calenda_first:

                break;
            case R.id.layout_calenda_second:

                break;
        }
    }


    private String getTime(Date date) {//可根据需要自行截取数据显示
        Log.d("getTime()", "choice date millis: " + date.getTime());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    @Override
    public void onItemClickListener(View view, int pos, List<String> myLiveLists) {
        myposition = pos;
        caiGouiLeftAdapter.setCheckedPosition(pos);
        CaiGouDetailFragment.getInstance(myLiveLists.get(pos));
    }
}